import json
import requests
from bs4 import BeautifulSoup


def get_data():
    headers = {
        'authority': 'chiedocover.ru',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'ru,en;q=0.9',
        # 'cookie': 'bToDUf=NFkwQzvUlJjxobVuEAiTWIKpheBRCy; tmp_user_id=395072dad66e6d227c80ec952ba91090dec64c7d17e03035241d3d0fdcda2c96a%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22tmp_user_id%22%3Bi%3A1%3Bs%3A50%3A%22xki4Q_usSoBsLZEpSvxCCfp-GGD1V0jfBcVd5CH5HXuSh_R6oV%22%3B%7D; _ym_uid=1710593224329136118; _ym_d=1711196257; tmr_lvid=3be913ac9628444581a356ea6ca5995f; tmr_lvidTS=1710593233968; moclients_url=https%3A%2F%2Fchiedocover.ru%2Fmodels3d%3Futm_referrer%3Dhttps%253A%252F%252Fwww.google.com%252F%26page%3D2; moclients_refer=https%3A%2F%2Fchiedocover.ru%2Fmodels3d%3Futm_referrer%3Dhttps%253A%252F%252Fwww.google.com%252F%26page%3D2; _csrf=a819a635764b91b52e28f55b534f2c7a6d0dfa560e972282c02f81126ef43366a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22Glc1nz4cDZYQ6Q3h9xe38mDjcv2PVzyx%22%3B%7D; unwp=HT_418yE5ON5kpEzviamnFZBQ29--PVO2K-OAUKlK6BSWsvjvdOekRHk20TuTM3sIigTPw6uv33g37tRNdd97w%3D%3D; zmry=d63364185f134337cb966e03cd1914c2; ym_client_id=1710593224329136118; _ym_isad=1; domain_sid=Gm2FCL9_hRKxBjT4CAR3r%3A1712402736591; tmr_detect=1%7C1712405033944; bToDUf_hits=4; cf_clearance=I6MEWQc.YXCrl5PFhKPHoinEzQ_y7oMCmCXh3W_8Wmw-1712428594-1.0.1.1-A8Sx7C8zjgU8G4XGhAo.MKTJcMfsxl8Tgsp0V1Ut6XFbJYhfaBlPRApuIqdickhVCGCKeBcnAovSsOH4kyorcA; NFkwQzvUlJjxobVuEAiTWIKpheBRCy=67d4ea7df357ea5c680999f0b0af5795-1712428596-1712428593',
        'referer': 'https://chiedocover.ru/models3d',
        'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "YaBrowser";v="24.1", "Yowser";v="2.5"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Linux"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36',
    }

    url = "https://chiedocover.ru/models3d.yml"
    r = requests.get(url, headers=headers)

    soup = BeautifulSoup(r.text, "lxml")

    furniture_items = soup.find_all("offer")

    data = {}
    for fi in furniture_items:

        try:
            model_id = fi["id"]
        except:
            model_id = "Нет id"

        try:
            href = fi.find("url").text.strip()
        except:
            href = "Нет ссылки"

        try:
            furniture_title = fi.find("name").text.strip()
        except:
            furniture_title = 'Нет названия'

        try:
            picture = fi.find("picture").text.strip()
        except:
            picture = 'Нет картинки'

        data[model_id] = {
            "furniture_title": furniture_title,
            "href": href,
            "picture": picture
        }

    with open("chiedocover.json", "w") as file:
        json.dump(data, file, indent=4, ensure_ascii=False)


def check_new_models():
    with open(f'chiedocover.json') as file:
        data = json.load(file)

    headers = {
        'authority': 'chiedocover.ru',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'ru,en;q=0.9',
        # 'cookie': 'bToDUf=NFkwQzvUlJjxobVuEAiTWIKpheBRCy; tmp_user_id=395072dad66e6d227c80ec952ba91090dec64c7d17e03035241d3d0fdcda2c96a%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22tmp_user_id%22%3Bi%3A1%3Bs%3A50%3A%22xki4Q_usSoBsLZEpSvxCCfp-GGD1V0jfBcVd5CH5HXuSh_R6oV%22%3B%7D; _ym_uid=1710593224329136118; _ym_d=1711196257; tmr_lvid=3be913ac9628444581a356ea6ca5995f; tmr_lvidTS=1710593233968; moclients_url=https%3A%2F%2Fchiedocover.ru%2Fmodels3d%3Futm_referrer%3Dhttps%253A%252F%252Fwww.google.com%252F%26page%3D2; moclients_refer=https%3A%2F%2Fchiedocover.ru%2Fmodels3d%3Futm_referrer%3Dhttps%253A%252F%252Fwww.google.com%252F%26page%3D2; _csrf=a819a635764b91b52e28f55b534f2c7a6d0dfa560e972282c02f81126ef43366a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22Glc1nz4cDZYQ6Q3h9xe38mDjcv2PVzyx%22%3B%7D; unwp=HT_418yE5ON5kpEzviamnFZBQ29--PVO2K-OAUKlK6BSWsvjvdOekRHk20TuTM3sIigTPw6uv33g37tRNdd97w%3D%3D; zmry=d63364185f134337cb966e03cd1914c2; ym_client_id=1710593224329136118; _ym_isad=1; domain_sid=Gm2FCL9_hRKxBjT4CAR3r%3A1712402736591; tmr_detect=1%7C1712405033944; bToDUf_hits=4; cf_clearance=I6MEWQc.YXCrl5PFhKPHoinEzQ_y7oMCmCXh3W_8Wmw-1712428594-1.0.1.1-A8Sx7C8zjgU8G4XGhAo.MKTJcMfsxl8Tgsp0V1Ut6XFbJYhfaBlPRApuIqdickhVCGCKeBcnAovSsOH4kyorcA; NFkwQzvUlJjxobVuEAiTWIKpheBRCy=67d4ea7df357ea5c680999f0b0af5795-1712428596-1712428593',
        'referer': 'https://chiedocover.ru/models3d',
        'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "YaBrowser";v="24.1", "Yowser";v="2.5"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Linux"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36',
    }

    url = "https://chiedocover.ru/models3d.yml"
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, "lxml")

    furniture_items = soup.find_all("offer")

    new_models = {}
    for fi in furniture_items:
        try:
            model_id = fi["id"]

        except:
            model_id = "Нет id"

        if model_id in data:
            continue
        else:

            try:
                href = fi.find("url").text.strip()
            except:
                href = "Нет ссылки"

            try:
                furniture_title = fi.find("name").text.strip()
            except:
                furniture_title = 'Нет названия'

            try:
                picture = fi.find("picture").text.strip()
            except:
                picture = 'Нет картинки'

            data[model_id] = {
                "furniture_title": furniture_title,
                "href": href,
                "picture": picture
            }

            new_models[model_id] = {
                "furniture_title": furniture_title,
                "href": href,
                "picture": picture
            }

    with open(f'chiedocover.json', 'w') as file:
        json.dump(data, file, indent=4, ensure_ascii=False)

    return new_models


def main():
    # get_data()
    print(check_new_models())


if __name__ == '__main__':
    main()
